package com.chujian.framework.util;

import cn.hutool.http.HttpUtil;

import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class UrlUs {

    /**
     * url解析
     *
     * @param urlStr
     * @return
     * @throws Exception
     */
    public static UrlInfo parseUrl(String urlStr) throws Exception {
        String deUrl = URLDecoder.decode(urlStr, "utf-8");

        URL url = new URL(deUrl);
        String query = url.getQuery();

        return new UrlInfo()
                .setUrl(deUrl)
                .setProtocol(url.getProtocol())
                .setHost(url.getHost())
                .setPort(url.getPort())
                .setPath(url.getPath())
                .setQuery(url.getQuery())
                .setParams(HttpUtil.decodeParamMap(query, StandardCharsets.UTF_8));
    }

}
